﻿/// <reference name="MicrosoftAjax.js" />
/*global $clearHandlers, $get, Sys, Type, DotNetLib */
Type.registerNamespace('DotNetLib');

DotNetLib.CheckBoxListRequiredFieldValidator = function(element) {
    DotNetLib.CheckBoxListRequiredFieldValidator.initializeBase(this, [element]);
    this.minimumRequired = null;
    this.maximumAllowed = null;
    this.controlToValidate = $get(element.controltovalidate);
};

DotNetLib.CheckBoxListRequiredFieldValidator.prototype = {
    get_MinimumRequired: function() { return this.minimumRequired; },
    set_MinimumRequired: function(value) { this.minimumRequired = value; },

    get_MaximumAllowed: function() { return this.maximumAllowed; },
    set_MaximumAllowed: function(value) { this.maximumAllowed = value; },

    initialize: function() {
        DotNetLib.CheckBoxListRequiredFieldValidator.callBaseMethod(this, 'initialize');

        //        this.onValidateHandler = Function.createDelegate(this, this.onValidateHandler);
        this.get_element().evaluationfunction = Function.createDelegate(this, this.onEvaluate);
        //        $addHandler(this.controlToValidate, 'checked', this.onValidateHandler);
    },

    dispose: function() {
        $clearHandlers(this.controlToValidate);
        DotNetLib.CheckBoxListRequiredFieldValidator.callBaseMethod(this, 'dispose');
    },

    onEvaluate: function() {
        var col, checked, checkboxcount, i;
        col = this.controlToValidate.getElementsByTagName("*");
        if (col !== null) {
            checked = 0;
            checkboxcount = 0;
            for (i = 0; i < col.length; i += 1) {
                if (col.item(i).tagName === 'INPUT') {
                    checkboxcount += 1;
                    if (col.item(i).checked) {
                        checked += 1;
                    }
                }
            }
            if (checked < this.minimumRequired) {
                return false;
            }
            if (this.maximumAllowed !== null && checked > this.maximumAllowed) {
                return false;
            }
            return true;
        }
    }
};
DotNetLib.CheckBoxListRequiredFieldValidator.registerClass('DotNetLib.CheckBoxListRequiredFieldValidator', Sys.UI.Control);

DotNetLib.CheckBoxRequiredFieldValidator = function(element) {
    DotNetLib.CheckBoxRequiredFieldValidator.initializeBase(this, [element]);
    this.requiredValue = null;
    this.controlToValidate = $get(element.controltovalidate);
};
DotNetLib.CheckBoxRequiredFieldValidator.prototype = {
    get_RequiredValue: function() { return this.requiredValue; },
    set_RequiredValue: function(value) { this.requiredValue = value; },

    initialize: function() {
        DotNetLib.CheckBoxRequiredFieldValidator.callBaseMethod(this, 'initialize');

        //        this.onValidateHandler = Function.createDelegate(this, this.onValidateHandler);
        this.get_element().evaluationfunction = Function.createDelegate(this, this.onEvaluate);
        //        $addHandler(this.controlToValidate, 'checked', this.onValidateHandler);
    },

    dispose: function() {
        $clearHandlers(this.controlToValidate);
        DotNetLib.CheckBoxRequiredFieldValidator.callBaseMethod(this, 'dispose');
    },

    onEvaluate: function() {
        return this.controlToValidate.checked === this.requiredValue;
    }
};
DotNetLib.CheckBoxRequiredFieldValidator.registerClass('DotNetLib.CheckBoxRequiredFieldValidator', Sys.UI.Control);
